/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-07
 * Time: 15:58
 */

class Solution {
    public int countKDifference(int[] nums, int k) {
        int[] cnts = new int[101];
        int ans = 0;
        for(int num: nums) {
            if(num + k < 101)
                ans += cnts[num + k];
            if(num - k > 0)
                ans += cnts[num - k];
            cnts[num]++;
        }
        return ans;
    }
}


class Solution1 {
    //也就是这个数在j-1前出显得次数
    public int numIdenticalPairs(int[] nums) {
        int[] hash = new int[101];
        int ans =0;
        for(int j =0;j<nums.length;++j){
            ans += hash[nums[j]];
            hash[nums[j]]++;
        }
        return ans;
    }
}

public class TestDemo {

//    public static void main(String[] args) {
//        Solution1 solution = new Solution1();
//        int[] nums = {1,1,1,1};
//       solution.numIdenticalPairs(nums);
//    }

    public static void main(String[] args) {
     Solution solution = new Solution();
     int[] nums = {3,2,1,5,4};
     solution.countKDifference(nums,2);
    }
}
